package com.iwdnb.gkgz.common.model.dto;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

/**
 * 波段近似值数据
 */
@Data
public class StockRangeApproximateData {
    private int count;
    private List<StockRangeDTO> rangeList;
    private StockRangeDTO latestRange;
    private BigDecimal avgPrice;
    private BigDecimal lowPrice;
    private BigDecimal highPrice;

    public StockRangeApproximateData(List<StockRangeDTO> rangeList) {
        if (CollectionUtils.isEmpty(rangeList)) {
            throw new RuntimeException("波段列表不能为空");
        }
        this.rangeList = rangeList;
        init();
    }

    public void init() {
        this.count = rangeList.size();
        lowPrice = rangeList.get(0).getLowPrice();
        highPrice = rangeList.get(rangeList.size() - 1).getHighPrice();
        List<BigDecimal> bigDecimalList = rangeList.stream().map(StockRangeDTO::getLowPrice).collect(
            Collectors.toList());
        avgPrice = bigDecimalList.stream().reduce(BigDecimal::add).orElse(new BigDecimal(0.00));
        avgPrice = BigDecimalUtils.divide(avgPrice, new BigDecimal(rangeList.size()));
        latestRange = rangeList.get(0);
        for (StockRangeDTO rangeDTO : rangeList) {
            if (DateUtil.parseDate(rangeDTO.getBeginDate()).after(DateUtil.parseDate(latestRange.getBeginDate()))) {
                latestRange = rangeDTO;
            }
        }
    }

}
